package com.example.teaculture.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.teaculture.R;
import com.example.teaculture.service.UserService;
import com.example.teaculture.model.User;

public class UserDataActivity extends AppCompatActivity {
    private String username, sex, userage;
    private int age;
    private TextView user_name;
    private EditText user_sex, user_age;
    private Button bt1, bt2;
    UserService userService = new UserService(UserDataActivity.this);
    User user = new User();


    public void initView() {
        String sex1;
        int age1;

        Intent intent = getIntent();
        username = intent.getStringExtra("Email");
        Log.i("TAG", "mail:" + username);
        user_name.setText(username);
        user.setUsername(username);

        user = userService.getUser(username, user);
        age1 = user.getAge();
        sex1 = user.getSex();

        user_sex.setText(sex1);
        user_age.setText(String.valueOf(age1));
        System.out.println(sex);
        System.out.println(age);


    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_data);
        user_age = (EditText) findViewById(R.id.age);
        user_name = (TextView) findViewById(R.id.Email);
        user_sex = (EditText) findViewById(R.id.sex);


        initView();
        bt1 = (Button) findViewById(R.id.updata);
        bt1.setOnClickListener(v -> {

            String sex2 = user_sex.getText().toString();
            String age2 = user_age.getText().toString();
            int age = Integer.parseInt(age2);
            user.setSex(sex2);
            user.setAge(age);
            userService.update(user);
            Toast.makeText(UserDataActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
        });
        bt2 = (Button) findViewById(R.id.comeback);
        bt2.setOnClickListener(v -> finish());

    }


}

